package com.IR.user.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.IR.shop.mapper.CarsMapper;
import com.IR.shop.mapper.NormsMapper;
import com.IR.shop.mapper.ProductMapper;
import com.IR.shop.po.Cars;
import com.IR.shop.po.CarsExample;
import com.IR.shop.po.Product;

@Service
public class ACarsBiz {
	@Resource(name="carsmapper")
	public CarsMapper mapper;
	@Resource
	public ProductMapper mapper1;
	@Resource
	public NormsMapper mapper2;
	private Cars cars;
	private List<Cars> list;
	public Cars getCars() {
		return cars;
	}
	public void setCars(Cars cars) {
		this.cars = cars;
	}
	public List<Cars> getList() {
		return list;
	}
	public void setList(List<Cars> list) {
		this.list = list;
	}

	/*通过用户id添加购物车内商品数量*/
	public void updateCarPronumberAdd(Long userid,Long proid) {
		CarsExample example=new CarsExample();
		CarsExample.Criteria c=example.createCriteria();
		c.andUseridEqualTo(userid);
		c.andProidEqualTo(proid);
		List<Cars> cars=mapper.selectByExample(example);
		for (Cars cc:cars) {
			cc.setPronumber(cc.getPronumber()+1);
			mapper.updateByExample(cc, example);
		}
	}
	/*通过用户id减少购物车内商品数量*/
	public void updateCarPronumberDecrease(Long userid,Long proid) {
		CarsExample example=new CarsExample();
		CarsExample.Criteria c=example.createCriteria();
		c.andUseridEqualTo(userid);
		c.andProidEqualTo(proid);
		List<Cars> cars=mapper.selectByExample(example);
		for (Cars cc:cars) {
			cc.setPronumber(cc.getPronumber()-1);
			mapper.updateByExample(cc, example);
		}
	}
	
	public void saveCar(Long userid,Long proid,int normsid) {
		Cars car=new Cars();
		car.setProid(proid);
		car.setPronumber(1);
		car.setUserid(userid);
		car.setNormsid(normsid);
		Product p=new Product();
		p=mapper1.selectByPrimaryKey(proid);
		car.setProimgs(p.getProimgs());
		car.setProname(p.getProname());
		car.setPronumber(1);
		car.setProprice(p.getProprice());
		mapper.insert(car);
	}
	public void updateCount() {
		cars=mapper.selectByPrimaryKey(1);
		int pronumber=2;
		cars.setPronumber(pronumber+1);
		mapper.updateByPrimaryKey(cars);
	}
	public void updateNorms() {
		cars=mapper.selectByPrimaryKey(6);
		int normsid=2;
		cars.setNormsid(normsid);
		mapper.updateByPrimaryKey(cars);
	}
	public void deleteCar() {
		Long[] l= {3l,4l,5l};	
		for(int i=0; i<l.length; i++){
			CarsExample example=new CarsExample();
			CarsExample.Criteria c=example.createCriteria();
			c.andProidEqualTo(l[i]);
			mapper.deleteByExample(example);
			
		}
	}
}
